<template>
  <div>
    <el-dialog :title="info.title" :visible.sync="info.isShow">
      <el-form :model="cateOne">
        <el-form-item label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="cateOne.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in cate"
              :value="item.id"
              :key="item.id"
              :label="item.catename"
            >
            </el-option>
          </el-select> 
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="cateOne.catename"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload action="#" list-type="picture-card" :auto-upload="false">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
                ref="img"
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <i class="el-icon-download"></i>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="cateOne.status"
            active-color="blue"
            inactive-color="grey"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="info.isAdd" @click="confirm"
          >确 定</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { cateAdd, cateOne } from "../../../request/api";
export default {
  props: ["info"],
  data() {
    return {
      menus: [],
      formLabelWidth: "70px",
      // element-ui图片上传
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
    };
  },
  computed: {
    ...mapGetters({
      cate: "cate/cate",
      cateOne: "cate/cateOne",
    }),
  },
  methods: {
    ...mapActions({
      cateAddActions: "cate/cateAddActions",
      cateListActions: "cate/cateListActions",
      cateOneActions: "cate/cateOneActions",
      cateUpdateActions: "cate/cateUpdateActions",
    }),
    //   取消弹框
    cancel() {
      this.info.isShow = false;
      // this.cateOne = {
      //   pid: "", //上级分类编号
      //   catename: "", //商品分类名称
      //   img: "", //图片(文件，一般用于二级分类)
      //   status: 1, // 状态 1.正常  2.禁用
      // };
    },
    update() {
      this.cateUpdateActions(this.cateOne);
      this.cateListActions();
      this.cancel();
    },
    confirm() {
      this.cateOne.img = this.$refs.img.src;
      this.cateAddActions(this.cateOne);
      this.cancel();
      this.cateListActions();
    },
    goDetail(id) {
      this.cateOneActions(id);
      this.cateOne.id = id;
      //  this.$refs.img.src=this.cateOne.img;
    },
    //   图片上传的方法
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
  mounted() {
    this.cateListActions();
  },
};
</script>

<style>
</style>